//
//  SZTabBarContentView.m
//  Pods
//
//  Created by Zahi on 2017/9/14.
//
//

#import "SZTabBarContentView.h"
#import <Masonry/Masonry.h>

@interface SZTabBarContentView ()

/// 标签滚动视图
@property (nonatomic, strong) UIScrollView *tabBarScrollView;
/// 内容容器滚动视图
@property (nonatomic, strong) UIScrollView *containerScrollView;

/// 选项按钮
@property (nonatomic, strong) NSMutableArray<UIButton *> *items;
/// 内容
@property (nonatomic, strong) NSMutableArray<UIView *> *contentViews;

@end

@implementation SZTabBarContentView


#pragma mark - 属性重写
- (NSMutableArray<UIButton *> *)items
{
    if (_items == nil) {
        
        _items = [NSMutableArray array];
    }
    return _items;
}

- (NSMutableArray<UIView *> *)contentViews
{
    if (_contentViews == nil) {
        
        _contentViews = [NSMutableArray array];
    }
    return _contentViews;
}

#pragma mark - 私有方法
- (NSInteger)numberOfItems {
    
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(numberOfItemsInTabBarContentView:)]) {
        
        return [self.dataSource numberOfItemsInTabBarContentView:self];
    }
    
    return 0;
}


- (void)reloadData
{

    // 默认标签条的高度
    CGFloat tabBarHeight = 30;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBarHeightInTabBarContentView:)]) {
        tabBarHeight = [self.delegate tabBarHeightInTabBarContentView:self];
        
    }
    
    if (self.tabBarScrollView == nil) {
        
        self.tabBarScrollView = [UIScrollView new];
        self.tabBarScrollView.backgroundColor = [UIColor brownColor];
        [self addSubview:self.tabBarScrollView];
        [self.tabBarScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.equalTo(self);
            make.height.mas_equalTo(tabBarHeight);
        }];
    } else {
        
        [self.tabBarScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.equalTo(self);
            make.height.mas_equalTo(tabBarHeight);
        }];
    }
    
    
    if (self.containerScrollView == nil) {
        
        self.containerScrollView = [UIScrollView new];
        
        self.containerScrollView.backgroundColor = [UIColor cyanColor];
        self.containerScrollView.pagingEnabled = YES;
        self.containerScrollView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:self.containerScrollView];
        
        [self.containerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.equalTo(self);
            make.top.equalTo(self.tabBarScrollView.mas_bottom);
        }];
        
    } else {
        [self.containerScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.equalTo(self);
            make.top.equalTo(self.tabBarScrollView.mas_bottom);
        }];
    }
    
    // 创建标签条上的按钮项
    NSInteger itemCount = [self numberOfItems];
    
    float precent = 1.0 / itemCount;
    
    // 循环创建按钮
    for (int i = 0; i < itemCount; i++) {
        UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
        
        // 设置button标题
        if (self.dataSource && [self.dataSource respondsToSelector:@selector(tabBarContentView:titleAtItemsIndex:)]) {
            
            NSString *title = [self.dataSource tabBarContentView:self titleAtItemsIndex:i];
            
            [item setTitle:title forState:UIControlStateNormal];
        }
        
        item.titleLabel.font = [UIFont systemFontOfSize:13];
        
        [self.tabBarScrollView addSubview:item];
        
        [self.items addObject:item];
    }
    
    for (int i = 0; i < itemCount; i++) {
        UIButton *item = self.items[i];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (i == 0) {// 第一项
                make.leading.equalTo(self.tabBarScrollView);
            }
            
            if (i == itemCount - 1) { // 最后一项
                make.trailing.equalTo(self.tabBarScrollView);
            }
            
            if (i > 0 && i < itemCount -1) {
                
                make.leading.equalTo(self.items[i-1].mas_trailing);
                make.trailing.equalTo(self.items[i+1].mas_leading);
            }
            
            make.top.bottom.height.equalTo(self.tabBarScrollView);
            make.width.equalTo(self.tabBarScrollView.mas_width).multipliedBy(precent);
        }];
    }
    
    // 内容
    for (int i = 0; i < itemCount; i++) {
        UIView *contentView = nil;
        
        if (self.dataSource && [self.dataSource respondsToSelector:@selector(tabBarContentView:contentViewAtItemsIndex:)]) {
            
            contentView = [self.dataSource tabBarContentView:self contentViewAtItemsIndex:i];
            
        } else {
            contentView = [UIView new];
        }
        
        [self.containerScrollView addSubview:contentView];
        [self.contentViews addObject:contentView];
        
    }
    
    // 布局内容
    for (int i = 0; i < itemCount; i++) {
        
        UIView *contentView = self.contentViews[i];
        
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (i == 0) {
                make.leading.equalTo(self.containerScrollView);
            }
            
            if (i == itemCount - 1) {
                make.trailing.equalTo(self.containerScrollView);
            }
            
            if (i > 0 && i < itemCount -1) {
                
                make.leading.equalTo(self.contentViews[i - 1].mas_trailing);
                make.trailing.equalTo(self.contentViews[i + 1].mas_leading);
            }
            make.top.bottom.width.height.equalTo(self.containerScrollView);
        }];
        
        
    }
}


@end
